﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Controls;
using Medianamik.UI.Web.Shop.Data;
using Medianamik.UI.Web.Shop.Data.Repositories;
using StructureMap;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.Shop.Controls
{
    public class CategoryCheckBoxTreeView: CheckBoxTreeView
    {
        //private RadTreeView _treeView = new RadTreeView()
        //{
        //    ID = "items",
        //    CheckBoxes = true,
        //};

        //protected override void CreateControlHierarchy(bool pUseDataSource)
        //{
        //    Controls.Add(_treeView);
        //    _treeView.EnableEmbeddedSkins = false;
        //    _treeView.Skin = "Medianamik";
        //    _treeView.NodeExpand += new RadTreeViewEventHandler(TreeNodeExpand);

        //    if (pUseDataSource)
        //    {
        //        foreach (Node option in RootNodes)
        //        {
        //            RadTreeNode nd = new RadTreeNode(option.DisplayName, option.ID.ToString());
        //            if (option.HasChildren)
        //                nd.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;

        //            if (!OptionNodeIds.Contains(option.ID) || (IsSyndication && ParentNode.LegitimateChildTypes.Any(t => t.ID.Equals(option.NodeTypeID))))
        //            {
        //                nd.Category = "Disabled";
        //                nd.CssClass += " rtDisabled";
        //                nd.Checkable = false;
        //                nd.HoveredCssClass = "rtDisabled";
        //                nd.SelectedCssClass = "rtDisabled";
        //            }

        //            _treeView.Nodes.Add(nd);
        //        }

        //        Guid[] selectedValues = (DataSource as IEnumerable<Guid> ?? Enumerable.Empty<Guid>()).ToArray();
        //        foreach (var selectedNode in NodeManager.GetNodes(selectedValues))
        //        {
        //            RadTreeNode treeNode = _treeView.FindNodeByValue(selectedNode.ID.ToString());

        //            if (treeNode == null)
        //            {
        //                List<Guid> parentIds = DataUtil.GetBottomToTopNodeIds(selectedNode.Path, true, 0);
        //                parentIds.Reverse();

        //                foreach (Guid id in parentIds)
        //                {
        //                    treeNode = _treeView.FindNodeByValue(id.ToString());
        //                    if (treeNode != null)
        //                        NodeExpand(treeNode);
        //                }

        //                treeNode = _treeView.FindNodeByValue(selectedNode.ID.ToString());
        //            }

        //            if (treeNode != null)
        //            {
        //                treeNode.Checked = true;
        //            }
        //        }

        //    }
        //}

        //private void NodeExpand(RadTreeNode parentNode)
        //{
        //    List<Node> childs = NodeManager.GetChildNodes(new Guid(parentNode.Value));
        //    childs.Sort((n1, n2) => n1.LongPosition.CompareTo(n2.LongPosition));
        //    childs.RemoveAll(n => n.NodeTypeID.Equals(MedianamikTypes.MedianamikSyndication));
        //    childs.RemoveAll(n => FilterNode(n));

        //    foreach (Node child in childs)
        //    {
        //        RadTreeNode nd = new RadTreeNode(child.DisplayName, child.ID.ToString());
        //        if (child.HasChildren)
        //            nd.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
        //        if (!OptionNodeIds.Contains(child.ID) || (IsSyndication && !ParentNode.LegitimateChildTypes.Any(t => t.ID.Equals(child.NodeTypeID))))
        //        {
        //            nd.Category = "Disabled";
        //            nd.CssClass += " rtDisabled";
        //            nd.Checkable = false;
        //            nd.HoveredCssClass = "rtDisabled";
        //            nd.SelectedCssClass = "rtDisabled";
        //        }
        //        parentNode.Nodes.Add(nd);
        //    }

        //    parentNode.ExpandMode = TreeNodeExpandMode.ClientSide;
        //    parentNode.Expanded = true;
        //}

        //void TreeNodeExpand(object sender, RadTreeNodeEventArgs e)
        //{
        //    NodeExpand(e.Node);
        //}

        private List<Node> _rootNodes;
        public override List<Node> RootNodes
        {
            get
            {
                if (_rootNodes == null)
                {
                    _rootNodes = NodeManager.GetChildNodes(ControlInfo.TreeRootNodeID.GetValueOrDefault(Modules.Shop));
                    _rootNodes.Sort((n1, n2) => n1.LongPosition.CompareTo(n2.LongPosition));
                    _rootNodes.RemoveAll(r => r.TypeId != Category.TYPE_ID && !r.NodeType.Inherits(Category.TYPE_ID));
                    _rootNodes.RemoveAll(FilterNode);
                }
                return _rootNodes;
            }
        }
    }
}
